package.path = "./plugin/MT53RS/utils.lua"..";..\\?.lua"
package.path = "./plugin/MT53RS/json.lua"..";..\\?.lua"

require "utils.lua"
require "json.lua"

rxBuf = {}
regAddr = 0x0000

function NewVariables()
    -- body
    local VariableMap = {
        Variable = {
        },
    }

    return VariableMap
end

function GenerateGetRealVariables(sAddr,step)

    -- body
    local crc
    local requestADU = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }

    if(step == 0)--一条命令step=0；第二条命令step=1；标识命令条数
    then
        ----------读-----------------
        requestADU[1] = sAddr
        requestADU[2] = 0x03
        requestADU[3] = 0x00
        requestADU[4] = 0x10
        requestADU[5] = 0x00
        requestADU[6] = 0x01
        local VariableMapTemp = {
            Variable = requestADU
        }
        crc = GetCRCModbus(VariableMapTemp)
        requestADU[7] = math.modf(crc%256)
        requestADU[8] = math.modf(crc/256)

        regAddr = 0x0010
        VariableMap = {
            Status = "0",
            Variable = requestADU
        }
    end


    return VariableMap
end

function DeviceCustomCmd(sAddr,cmdName,cmdParam,step)

    -- body
    local crc
    local requestADU = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }
    local temp = 0

    if(cmdName == "GetRealVariables")
    then
        if(step == 0)
        then
            ----------读运行参数-----------------
            requestADU[1] = sAddr
            requestADU[2] = 0x03
            requestADU[3] = 0x00
            requestADU[4] = 0x10
            requestADU[5] = 0x00
            requestADU[6] = 0x01
            local VariableMapTemp = {
                Variable = requestADU
            }
            crc = GetCRCModbus(VariableMapTemp)
            requestADU[7] = math.modf(crc%256)
            requestADU[8] = math.modf(crc/256)

            regAddr = 0x0010
            VariableMap = {
                Status = "0",
                Variable = requestADU
            }
        end 
    elseif(cmdName == "OpenRelay")
    then
        if(step == 0)
        then
            ----------分闸-----------------
            requestADU[1] = sAddr
            requestADU[2] = 0x06
            requestADU[3] = 0x00
            requestADU[4] = 0x11
            requestADU[5] = 0x00
            requestADU[6] = 0x01
            local VariableMapTemp = {
                Variable = requestADU
            }
            crc = GetCRCModbus(VariableMapTemp)
            requestADU[7] = math.modf(crc%256)
            requestADU[8] = math.modf(crc/256)

            regAddr = 0x0011
            VariableMap = {
                Status = "0",
                Variable = requestADU
            }
        end  
    elseif(cmdName == "CloseRelay")
    then
        if(step == 0)
        then
            ----------合闸-----------------
            requestADU[1] = sAddr
            requestADU[2] = 0x06
            requestADU[3] = 0x00
            requestADU[4] = 0x11
            requestADU[5] = 0x00
            requestADU[6] = 0x02
            local VariableMapTemp = {
                Variable = requestADU
            }
            crc = GetCRCModbus(VariableMapTemp)
            requestADU[7] = math.modf(crc%256)
            requestADU[8] = math.modf(crc/256)

            regAddr = 0x0011
            VariableMap = {
                Status = "0",
                Variable = requestADU
            }
        end                    
    elseif(cmdName == "SetVariables")
    then
        res = json.jsondecode(cmdParam)
        local ManualOnOff = 0
        if (res["RelayCmd"] ~= nil)
        then
            requestADU[1] = sAddr
            requestADU[2] = 0x06
            requestADU[3] = 0x00
            requestADU[4] = 0x11
            requestADU[5] = 0x00
            if(res["RelayCmd"] == "1" or res["RelayCmd"] == 1)then
                requestADU[6] = 0x01
            elseif(res["RelayCmd"] == "2" or res["RelayCmd"] == 2)then
                requestADU[6] = 0x02
            else
                requestADU[6] = 0x01
            end
            local VariableMapTemp = {
                Variable = requestADU
            }
            crc = GetCRCModbus(VariableMapTemp)
            requestADU[7] = math.modf(crc%256)
            requestADU[8] = math.modf(crc/256)

            regAddr = 0x0011
            VariableMap = {
                Status = "0",
                Variable = requestADU
            }
        end
    end

    return VariableMap
end

function AnalysisRx(sAddr,rxBufCnt)

    local index = 1
    local mAddr = 0
    local mFunCode = 0
    local mRegByte = 0
    local crcH,crcL,crc,crcTemp

    -- body
    local VariableMap = {
        Status = "1",
        Variable = {},
    }

    --modbus03 06 最短为8个字节
    if(rxBufCnt < 8)
    then
        rxBuf = {}
        return VariableMap
    end


    for index=1,rxBufCnt,1 do
        if(index <= rxBufCnt)
        then
            mAddr     = rxBuf[index]
            mFunCode  = rxBuf[index+1]
            if (mAddr == tonumber(sAddr))
            then
                if(mFunCode == 0x03)
                then
                    mRegByte = rxBuf[index+2]
                    if(mRegByte+5 > rxBufCnt)
                    then
                        rxBuf = {}
                        return VariableMap
                    end
                    --取出数据域数据
                    local pdu = {}
                    for i=index,index+2+mRegByte,1 do
                        if rxBuf[i] ~= nil then
                            table.insert(pdu, rxBuf[i])
                        end
                    end
                    local VariableMapCRC = {
                        Variable = pdu
                    }
                    crc = GetCRCModbus(VariableMapCRC)
                    crcTemp = rxBuf[index+3+mRegByte+1]*256+rxBuf[index+3+mRegByte]
                    if(crc == crcTemp)
                    then
                        local Variable = {}
                        local varIndex = 3
                        local VariableIndex = 1
                        if(regAddr == 0x0010)
                        then
                            ---------重合闸状态----------------------
                            local RelayStatus = rxBuf[DATAIndex]*256+rxBuf[DATAIndex+1]
                            DATAIndex = DATAIndex + 2
                            Variable[VariableIndex]  = utils.AppendVariable(VariableIndex,"RelayStatus",RelayStatus,"")
                            VariableIndex = VariableIndex + 1

                            VariableMap = {
                                Status = "0",
                                Variable = Variable
                            }
                        end
                        rxBuf = {}
                        return VariableMap
                    end
                elseif(mFunCode == 0x05)
                then
                    mRegAddr = rxBuf[index+2]*256+rxBuf[index+3]
                    --取出数据域数据
                    local pdu = {}
                    for i=index,index+5,1 do
                        if rxBuf[i] ~= nil then
                            table.insert(pdu, rxBuf[i])
                        end
                    end
                    local VariableMapCRC = {
                        Variable = pdu
                    }
                    crc = GetCRCModbus(VariableMapCRC)
                    crcTemp = rxBuf[index+6+1]*256+rxBuf[index+6]
                    if(crc == crcTemp)
                    then
                        VariableMap = {
                            Status = "0",
                            Variable = Variable
                        }
                        rxBuf = {}
                        return VariableMap
                    end
                elseif(mFunCode == 0x06)
                then
                    mRegAddr = rxBuf[index+2]*256+rxBuf[index+3]
                    --取出数据域数据
                    local pdu = {}
                    for i=index,index+5,1 do
                        if rxBuf[i] ~= nil then
                            table.insert(pdu, rxBuf[i])
                        end
                    end
                    local VariableMapCRC = {
                        Variable = pdu
                    }
                    crc = GetCRCModbus(VariableMapCRC)
                    crcTemp = rxBuf[index+6+1]*256+rxBuf[index+6]
                    if(crc == crcTemp)
                    then
                        VariableMap = {
                            Status = "0",
                            Variable = Variable
                        }
                        rxBuf = {}
                        return VariableMap
                    end    
                else
                    index = index + 1
                end
            else
                index = index + 1
            end
        else
            rxBuf = {}
            return VariableMap
        end
    end

    rxBuf = {}
    return VariableMap
end
